//
//  MHSettingViewModel.m
// MiaoCircle
//
//Created by acgera on 2018/4/9.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSettingViewModel.h"
#import "MHCommonArrowItemViewModel.h"
//#import "MHPlugViewModel.h"
#import "MHAccountSecurityViewModel.h"
//#import "MHNotificationViewModel.h"
#import "MHWebViewModel.h"
//#im
//#import "MHAboutUsViewModel.h"
//#import "MHPrivacyViewModel.h"
//#import "MHGeneralViewModel.h"
@interface MHSettingViewModel ()
/// 登出的命令
@property (nonatomic, readwrite, strong) RACCommand *logoutCommand;
@end

@implementation MHSettingViewModel
- (void)initialize{
    [super initialize];
    @weakify(self);
    self.title = @"设置";
   
    /// 第一组
    MHCommonGroupViewModel *group0 = [MHCommonGroupViewModel groupViewModel];
    ///账号与安全
    MHCommonArrowItemViewModel *accountSecurity = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"账号与安全"];
    accountSecurity.destViewModelClass = [MHAccountSecurityViewModel class];
    MHCommonArrowItemViewModel *identify = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"实名认证"];
    group0.itemViewModels = @[ accountSecurity,identify];
    group0.header = @"账户";
    group0.headerHeight = MHTableViewFooterHeaderHeight(group0.header);

    
    
    
    
    /// 第二组
    MHCommonGroupViewModel *group1 = [MHCommonGroupViewModel groupViewModel];
    /// 新消息通知
//    MHCommonArrowItemViewModel *messageNote = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"新消息通知"];
//    messageNote.destViewModelClass = [MHNotificationViewModel class];
//    /// 隐私
//    MHCommonArrowItemViewModel *private = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"隐私"];
//    private.destViewModelClass = [MHPrivacyViewModel class];
//
//    /// 通用
//    MHCommonArrowItemViewModel *general = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"通用"];
//    general.destViewModelClass = [MHGeneralViewModel class];
//    group1.itemViewModels = @[ messageNote , private , general];
    
    /// 第三组
    MHCommonGroupViewModel *group2 = [MHCommonGroupViewModel groupViewModel];
    /// 帮助与反馈
    MHCommonArrowItemViewModel *help = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"帮助与反馈"];
    
  
    help.operation = ^{
//        @strongify(self);
//        NSURL *url = [NSURL URLWithString:MHMyBlogHomepageUrl];
//        NSURLRequest *request = [NSURLRequest requestWithURL:url];
//        MHWebViewModel * viewModel = [[MHWebViewModel alloc] initWithServices:self.services params:@{MHViewModelRequestKey:request}];
//        /// 去掉关闭按钮
//        viewModel.shouldDisableWebViewClose = YES;
//        [self.services pushViewModel:viewModel animated:YES];
    };
    
    group2.itemViewModels = @[ help ];

    group2.header = @"设置";
    group2.headerHeight = MHTableViewFooterHeaderHeight(group2.header);
    
    group2.footerHeight = 200;
    
    /// 第四组
//    MHCommonGroupViewModel *group3 = [MHCommonGroupViewModel groupViewModel];
    /// 插件
//    MHCommonArrowItemViewModel *plug = [MHCommonArrowItemViewModel itemViewModelWithTitle:@"插件"];
//    plug.centerLeftViewName = @"WeChat_Lab_Logo_small_15x17";
//    plug.destViewModelClass = [MHPlugViewModel class];
//    group3.itemViewModels = @[ plug ];
    
    self.dataSource = @[group0  , group2 ];
    
    /// 退出登录的命令
    self.logoutCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        /// 删除账号
        [SAMKeychain deleteRawLogin];
        /// 先退出用户
        [[self.services client] logoutUser];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            /// 延迟一段时间
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                /// 这里切换 到账号登录的界面
                [MHNotificationCenter postNotificationName:MHSwitchRootViewControllerNotification object:nil userInfo:@{MHSwitchRootViewControllerUserInfoKey:@(MHSwitchRootViewControllerFromTypeLogout)}];
                [subscriber sendNext:nil];
                [subscriber sendCompleted]; 
            });
            return [RACDisposable disposableWithBlock:^{
                
            }];
        }];
    }];
    
    
}
@end
